package com.zzzzzz.content.pictext;

import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zzzzzz.account.signin.NoSuchOnlineUserException;
import com.zzzzzz.account.signin.OnlineService;
import com.zzzzzz.common.message.Message;
import com.zzzzzz.common.message.MessageIcon;
import com.zzzzzz.common.page.PageStart;
import com.zzzzzz.content.picitem.PicItemListView;
import com.zzzzzz.content.picitem.PicItemService;

@Controller
public class PicTextController {
	@Resource
	private PicTextService picTextService;
	@Resource
	private PicItemService picItemService;
	@Resource
	private OnlineService onlineService;
	
	@RequestMapping(value = "/pictext/add", method = RequestMethod.GET)
	public String addView(@ModelAttribute PicTextForm picTextForm) {
		return "content/pictext/add";
	}
	
	@RequestMapping(value = "/pictext/add", method = RequestMethod.POST)
	public String add(Model model, @Valid PicTextForm picTextForm, BindingResult bindingResult, RedirectAttributes redirectAttributes, Locale locale) throws NoSuchOnlineUserException {
		if (bindingResult.hasErrors()) {
			return addView(picTextForm);
		}
		
		Long userId = onlineService.findUserId();
		try {
			Long picTextId = picTextService.add(userId, picTextForm);
			return "redirect:/picitem/upload/" + picTextId;
		} catch (Exception e) {
			logger.warn("pictextAdd", e);
			redirectAttributes.addFlashAttribute(Message.error(MessageIcon.WARNING + "添加失败"));
			return "redirect:/pictext/add";
		}
	}
	
	@RequestMapping(value="/pictext/{picTextId}", method=RequestMethod.GET)
	public String view(@PathVariable Long picTextId, 
			@RequestParam(value = "start", defaultValue = "0", required = false) Integer start, 
			HttpServletRequest request, Model model) throws Exception {
		
		List<PicItemListView> picItemViewList = picItemService.findByPicTextId(picTextId, start, PageStart.DEFAULT_OFFSET);
		if(picItemViewList!=null && picItemViewList.size()>0){
			picTextService.plusLooks(picTextId);
		}
		
		Long count = picItemService.findByPicTextIdCount(picTextId);
		PageStart pageStart = new PageStart(start, PageStart.DEFAULT_OFFSET, count, "/");
		
		model.addAttribute("picItemViewList", picItemViewList);
		model.addAttribute("count", count);
		model.addAttribute("pageBar", pageStart.getPageBar());
		
		return "content/picitem/list";
	}
	
	@RequestMapping(value="/pictext/{picTextId}/seq", method=RequestMethod.GET)
	public String seqView(@PathVariable Long picTextId, 
			@RequestParam(value = "start", defaultValue = "0", required = false) Integer start, 
			HttpServletRequest request, Model model) throws Exception {
		
		List<PicItemListView> picItemViewList = picItemService.findByPicTextId(picTextId, start, 1000);
		Long count = picItemService.findByPicTextIdCount(picTextId);
		
		model.addAttribute("picTextId", picTextId);
		model.addAttribute("picItemViewList", picItemViewList);
		model.addAttribute("count", count);
		
		return "content/picitem/listSeq";
	}
	
	private static final Logger logger = LoggerFactory.getLogger(PicTextController.class);
}
